package com.cl.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.core.dto.PagingReqDTO;
import com.cl.core.mapper.PlatformUserMapper;
import com.cl.core.pojo.PlatformUsers;
import com.cl.core.service.UserServices;
import com.cl.utility.constant.PassInfo;
import com.cl.utility.util.DateUtils;
import com.cl.utility.util.GenerateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author liddhappy
 * @description menuServices 实现
 * @createTime 2022/12/3 21:05
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<PlatformUserMapper, PlatformUsers> implements UserServices {

    @Autowired
    PlatformUserMapper userMapper;


    @Override
    public void addUser(PlatformUsers user) {
        log.info("add user " + user);
        PassInfo passInfo = GenerateUtils.buildPassword(user.getPassword());
        user.setPassword(passInfo.getPassword());
        user.setSalt(passInfo.getSalt());
        user.setCreateTime(DateUtils.getNowDate());
        user.setUpdateTime(DateUtils.getNowDate());
        userMapper.insertSelective(user);
        log.info("add user success");
    }

    @Override
    public void deleteUser(String userId) {
        log.info("deleteUserByUserId " + userId);
        Long convert = !StringUtils.isEmpty(userId) ? (Long) ConvertUtils.convert(userId, Long.class) : Long.MIN_VALUE;
        userMapper.deleteByPrimaryKey(convert);
        log.info("deleteUserByUserId  success");
    }

    @Override
    public void updateUser(PlatformUsers user) {
        PlatformUsers platformUsers = userMapper.selectByPrimaryKey(user.getUserId());
        platformUsers.setUserName(user.getUserName());
        PassInfo passInfo = GenerateUtils.buildPassword(user.getPassword());
        platformUsers.setPassword(passInfo.getPassword());
        platformUsers.setSalt(passInfo.getSalt());
        platformUsers.setRoleId(user.getRoleId());
        platformUsers.setState(user.getState());
        //更新时间
        user.setUpdateTime(DateUtils.getNowDate());
        platformUsers.setUpdateTime(user.getUpdateTime());
        log.info("updateUser  user " + platformUsers);
        userMapper.updateByPrimaryKey(platformUsers);
        log.info("updateUser  success");
    }

    @Override
    public PlatformUsers selectUser(String userId) {
        log.info("selectUser  userId " + userId);
        Long convert = !StringUtils.isEmpty(userId) ? (Long) ConvertUtils.convert(userId, Long.class) : Long.MIN_VALUE;
        PlatformUsers platformUsers = userMapper.selectByPrimaryKey(convert);
        log.info("selectUser  result  " + platformUsers);
        log.info("selectUser  success");
        return platformUsers;
    }

    /**
     * @param reqDTO 分页查询请求
     * @return 考试对象
     */
    @Override
    public IPage<PlatformUsers> paging(PagingReqDTO<PlatformUsers> reqDTO) {

        //创建分页对象
        Page page = new Page(reqDTO.getCurrent(), reqDTO.getSize());

        //转换结果
        return baseMapper.paging(page, reqDTO.getParams());
    }


}
